﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Base;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.DAO.Admin
{
    public class MenuDAO : SqlDAO
    {
        public const string SP_CREATE_OR_UPDATE_MENU = "spCreateOrUpdateMenu";
        public const string SP_GET_LIST_PAGING_MENU = "spGetListPagingMenu";
        public const string SP_GET_MENU_BY_ID = "spGetMenuById";
        public const string SP_DELETE_MENU = "spDeleteMenu";
        public const string SP_GET_MENU_BY_APP_ID = "spGetAllMenuByAppId";
        public const string SP_GET_MENU_BY_PARENT_ID = "spGetAllMenuByParentId";
        public const string SP_GET_MENU_BY_APP_CODE = "spGetAllMenuByAppCode";
        public const string SP_GET_MENU_BY_USER_ID_APP_CODE = "spGetAllMenuByUserIdAppCode";
        public const string SP_CHECK_EXIST_MENU = "spCheckExistMenu";
        public const string SP_ACTIVE = "spActiveMenu";
        public const string Param_MenuId = "@MenuId";
        public const string Param_UserId = "@UserId";
        public const string Param_AppId = "@AppId";
        public const string Param_ParentId = "@ParentId";
        public const string Param_AppCode = "@AppCode";
        public const string Param_MenuName = "@MenuName";
        public const string Param_MenuCode = "@MenuCode";
        public const string Param_RoleID = "@RoleID";
        public const string Param_MenuUrl = "@MenuUrl";
        public const string Param_IsActive = "@IsActive";

        public MenuDAO(String connectionString, System.Type type = null)
            : base(connectionString, type) { }

        internal Common.DataPage<MenuModel> GetDynamicListPaging(Models.Admin.MenuModel model, int pageSize, int pageIndex, Dictionary<string, string> mapDictionary, bool compareEqual)
        {
            try
            {
                //SqlCommand command = SharedSqlDao.GetSprocCommand(pr_Person_GetAll);
                SqlCommand command = this.GetSprocCommand(SP_GET_LIST_PAGING_MENU);
                if (model != null)
                {
                    command.Parameters.Add(CreateParameter(Param_AppId, model.AppId));
                    command.Parameters.Add(new SqlParameter(Param_ParentId, model.ParentMenuId));
                    command.Parameters.Add(CreateParameter(Param_MenuName, (compareEqual || String.IsNullOrEmpty(model.MenuName)) ? model.MenuName : ("%" + model.MenuName + "%")));
                    command.Parameters.Add(CreateParameter(Param_MenuCode, (compareEqual || String.IsNullOrEmpty(model.MenuCode)) ? model.MenuCode : ("%" + model.MenuCode + "%")));
                    command.Parameters.Add(CreateParameter(Param_MenuUrl, (compareEqual || String.IsNullOrEmpty(model.MenuUrl)) ? model.MenuUrl : ("%" + model.MenuUrl + "%")));
                    command.Parameters.Add(CreateParameter(Param_IsActive, model.IsActive));
                }
                command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
                command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
                return GetDataPage<MenuModel>(command, pageIndex, pageSize, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal MenuModel GetById(int? menuId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_MENU_BY_ID);
                command.Parameters.Add(new SqlParameter(Param_MenuId, menuId));

                return GetSingle<MenuModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal bool createOrUpdateMenu(MenuModel menuModel)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_CREATE_OR_UPDATE_MENU);
                command.Parameters.Add(new SqlParameter("@" + MenuModel.FIELD_MENU_NAME, menuModel.MenuName));
                command.Parameters.Add(new SqlParameter("@" + MenuModel.FIELD_MENU_CODE, menuModel.MenuCode));
                command.Parameters.Add(new SqlParameter("@" + MenuModel.FIELD_MENU_DESCRIPTION, menuModel.MenuDescription));
                command.Parameters.Add(new SqlParameter("@" + MenuModel.FIELD_MENU_URL, menuModel.MenuUrl));

                command.Parameters.Add(new SqlParameter("@" + MenuModel.FIELD_MENU_ORDER, menuModel.MenuOrder));

                command.Parameters.Add(new SqlParameter("@" + MenuModel.FIELD_CLASS_NAME, menuModel.ClassName));
                command.Parameters.Add(new SqlParameter("@" + MenuModel.FIELD_PARENT_MENU_ID, menuModel.ParentMenuId));
                command.Parameters.Add(new SqlParameter("@" + MenuModel.FIELD_IS_ACTIVE, menuModel.IsActive));
                command.Parameters.Add(new SqlParameter("@" + MenuModel.FIELD_APP_ID, menuModel.AppId));
                command.Parameters.Add(new SqlParameter("@" + MenuModel.FIELD_MENU_ID, menuModel.MenuId));
                command.Parameters.Add(new SqlParameter("@" + MenuModel.FIEL_ROLE_ID, menuModel.RoleID));
                //command.Parameters.Add(new SqlParameter("@" + MenuModel.FIELD_CLASS_NAME, menuModel.ClassName));
                return ExecuteQuery(command) > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        internal bool deleteMenu(int[] menuId)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_DELETE_MENU);
                command.Parameters.Add(new SqlParameter("@TableId", GetTableId<int>(menuId)));
                return ExecuteQuery(command) > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }

        internal List<MenuModel> GetAllMenuByAppId(int appId, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_MENU_BY_APP_ID);
                command.Parameters.Add(CreateParameter(Param_AppId, appId));
                return GetList<MenuModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal List<MenuModel> GetAllMenuByParentID(int intAppID, int intParentID, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_MENU_BY_PARENT_ID);
                command.Parameters.Add(CreateParameter(Param_AppId, intAppID));
                command.Parameters.Add(CreateParameter(Param_ParentId, intParentID));
                return GetList<MenuModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal List<MenuModel> GetAllMenuByAppCode(string appCode, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_MENU_BY_APP_CODE);
                command.Parameters.Add(CreateParameter(Param_AppCode, appCode));
                return GetList<MenuModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal IEnumerable<MenuModel> GetAllMenuByUserIdAndAppCode(int userId, string appCode, Dictionary<string, string> mapDictionary)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(SP_GET_MENU_BY_USER_ID_APP_CODE);
                command.Parameters.Add(CreateParameter(Param_UserId, userId));
                command.Parameters.Add(CreateParameter(Param_AppCode, appCode));
                return GetList<MenuModel>(command, mapDictionary);
            }
            catch
            {
                throw;
            }
        }

        internal bool checkMenuExist(int menuId, string menuCode, string menuName, string menuUrl, int appId)
        {
            SqlCommand command = this.GetSprocCommand(SP_CHECK_EXIST_MENU);
            command.Parameters.Add(new SqlParameter(Param_MenuCode, menuCode));
            command.Parameters.Add(new SqlParameter(Param_AppId, appId));
            command.Parameters.Add(new SqlParameter(Param_MenuName, menuName));
            command.Parameters.Add(new SqlParameter(Param_MenuUrl, menuUrl));
            command.Parameters.Add(new SqlParameter(Param_MenuId, menuId));
            //command.Parameters.Add(new SqlParameter(Param_RecordCount, null));
            var returnParameter = command.Parameters.Add("@ReturnVal", System.Data.SqlDbType.Int);
            returnParameter.Direction = System.Data.ParameterDirection.ReturnValue;
            ExecuteNonQuery(command);
            var result = returnParameter.Value;
            return Convert.ToInt32(result) > 0 ? true : false;
        }

        internal bool activeMenu(string id)
        {
            try
            {
                SqlCommand command = this.GetSprocCommand(MenuDAO.SP_ACTIVE);
                int Menuids = Convert.ToInt32(id);
                command.Parameters.Add(CreateParameter(MenuDAO.Param_MenuId, Menuids));

                int result = ExecuteQuery(command);
                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }
        }
    }
}
